package com.huawei.hms.framework.network.restclient.hwhttp.okhttp;

import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.hianalytics.BaseRequestFinishedInfo;
import com.huawei.hms.framework.network.restclient.hianalytics.EditableMetrics;
import com.huawei.hms.framework.network.restclient.hianalytics.EditableMetricsTime;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import java.net.MalformedURLException;
import java.net.URL;

public class OkhttpConnRequestFinishedInfo extends BaseRequestFinishedInfo {
    private static final String TAG = "OkhttpConnRequestFinish";
    private String host;
    private EditableMetrics metrics = new EditableMetrics();
    private EditableMetricsTime metricsRealTime = new OkhttpConnMetricsTime(true);
    private EditableMetricsTime metricsTime = new OkhttpConnMetricsTime(false);
    private String url;

    class OkhttpConnMetricsTime extends EditableMetricsTime {
        OkhttpConnMetricsTime(boolean z) {
            super(z);
        }

        public long getTotalTime() {
            return getAndCheckEndTime(getCallStartTime(), getCallEndTime()) - getCallStartTime();
        }

        public long getTtfb() {
            return getAndCheckEndTime(getCallStartTime(), this.ttfb) - getCallStartTime();
        }
    }

    OkhttpConnRequestFinishedInfo() {
    }

    public void setUrl(String str) {
        this.url = str;
        try {
            this.host = new URL(str).getHost();
        } catch (MalformedURLException e) {
            Logger.i(TAG, "fail to get hostname from url");
        }
    }

    public EditableMetricsTime getMetricsTime() {
        return this.metricsTime;
    }

    public EditableMetrics getMetrics() {
        return this.metrics;
    }

    public EditableMetricsTime getMetricsRealTime() {
        return this.metricsRealTime;
    }

    public String getNetworkSdkType() {
        return HttpContants.TYPE_OKHTTP;
    }

    public String getUrl() {
        return this.url;
    }

    public String getHost() {
        return this.host;
    }
}
